"""
@file: models.py
@author: lingdubing
@time: 2024/11/04  16:16
@desc: 数据库模型
@character: utf-8
"""

from sqlmodel import SQLModel, Field, Relationship
from datetime import datetime
from sqlalchemy import JSON, Column


class TimeStampModel(SQLModel):
    create_time: datetime = Field(default=datetime.utcnow())
    update_time: datetime = Field(default=datetime.utcnow())


class UserRoleLink(TimeStampModel, table=True):
    user_id: str = Field(foreign_key="user.user_id", primary_key=True)
    role_id: str = Field(foreign_key="role.role_id", primary_key=True)

    user: "User" = Relationship(back_populates="user_role_links")
    role: "Role" = Relationship(back_populates="user_links")


class RoleAccessLink(TimeStampModel, table=True):
    role_id: str = Field(foreign_key="role.role_id", primary_key=True)
    access_id: str = Field(foreign_key="access.access_id", primary_key=True)

    role: "Role" = Relationship(back_populates="access_links")
    access: "Access" = Relationship(back_populates="access_role_links")


class User(TimeStampModel, table=True):
    # 本身属性
    user_id: str = Field(primary_key=True, description="用户编号,用户的唯一标识")
    username: str = Field(description="用户的电话号码", index=True)
    password: str = Field(description="用户的密码")
    user_status: int = Field(description="用户的状态,1为可用,0为被禁用,2表示正在骑行", default=1, index=True)

    # 附加属性
    user_role_links: list[UserRoleLink] = Relationship(back_populates="user")
    records: list["Record"] = Relationship(back_populates="user")
    feedbacks: list["Feedback"] = Relationship(back_populates="user")


class Role(TimeStampModel, table=True):
    # 本身属性
    role_id: str = Field(primary_key=True, description="角色编号,角色的唯一标识")
    role_name: str = Field(description="角色名")
    role_desc: str | None = Field(description="角色描述", default=None)

    # 附加属性
    user_links: list[UserRoleLink] = Relationship(back_populates="role")
    access_links: list[RoleAccessLink] = Relationship(back_populates="role")


class Access(TimeStampModel, table=True):
    # 本身属性
    access_id: str = Field(primary_key=True, description="权限编号，权限唯一的标识")
    access_name: str = Field(description="权限名称")
    access_desc: str | None = Field(description="权限描述", default=None)
    access_scopes: str | None = Field(default=None, description="权限范围标识")
    access_url: str | None = Field(default=None, description="权限对应的url")
    parent_id: str | None = Field(default=None, description="父亲编号")
    is_menu: bool = Field(default=False, description="是否为菜单")
    is_verify: bool = Field(default=False, description="是否需要验证")

    access_role_links: list[RoleAccessLink] = Relationship(back_populates="access")


class Area(TimeStampModel, table=True):
    # 本身属性
    area_id: str = Field(primary_key=True, description="区域编号，区域的唯一标识")
    area_name: str | None = Field(description="区域名称", default=None)
    area_desc: str | None = Field(description="区域描述", default=None)

    # 附加属性
    machines: list["Machine"] = Relationship(back_populates="area")


class Machine(TimeStampModel, table=True):
    # 本身属性
    machine_id: str = Field(primary_key=True, description="电动车编号,电动车的唯一标识")
    machine_point: dict | None = Field(default=None, description="电动车位置", sa_column=Column(JSON))
    machine_battery: int = Field(default=100, description="电动车电量")
    status: int = Field(description="车辆状态,0表示正在骑行中，1表示空闲中，2表示已损坏，3表示停止中", default=1)

    # 附加属性
    area_id: str = Field(foreign_key="area.area_id")
    area: Area | None = Relationship(back_populates="machines")

    records: list["Record"] = Relationship(back_populates="machine")
    feedbacks: list["Feedback"] = Relationship(back_populates="machine")


class Record(TimeStampModel, table=True):
    # 本身属性
    record_id: str = Field(primary_key=True, description="记录编码")
    start_time: datetime | None = Field(description="开始的时间", default=None)
    end_time: datetime | None = Field(description="结束的时间", default=datetime.utcnow())
    stop_time: int = Field(description="中途停止时间", default=0)
    consume_battery: int = Field(description="消耗的电量", default=0)
    tracejectory: dict | None = Field(description="轨迹", sa_column=Column(JSON), default=None)

    # 附加属性
    user_id: str = Field(foreign_key="user.user_id")
    user: User | None = Relationship(back_populates="records")
    machine_id: str = Field(foreign_key="machine.machine_id")
    machine: Machine | None = Relationship(back_populates="records")


class Feedback(TimeStampModel, table=True):
    # 本身属性
    feedback_id: str = Field(primary_key=True, description="反馈编号")
    feedback_msg: str = Field(description="反馈的消息", default=" ")
    is_solve: bool = Field(description="是否解决", default=False)

    # 附加属性
    user_id: str = Field(foreign_key="user.user_id")
    user: User | None = Relationship(back_populates="feedbacks")
    machine_id: str = Field(foreign_key="machine.machine_id")
    machine: Machine | None = Relationship(back_populates="feedbacks")
